#-*- coding: utf-8 -*-
'''
Created on Aug 22, 2012

@author: bbg
'''
from sqlalchemy import Integer, Column, String, MetaData, Table
from sqlalchemy.orm import mapper


class User(object):
    '''
    公司的员工信息，这个系统的使用者
    '''
    
    _Mapper_ = None
    _company_id_ = -1    
    
    def __init__(self):
        self.name = ""
        self.pwd = ""
        self.realname = ""
        self.shop_id = 0
        self.phone = ""
        self.email = ""
        self.right_level=0
    
    @staticmethod
    def doMapping(company_id):
        '''
        按company_id分库，每次生成对象前都要执行一次，以便map到正确的数据库上去
        '''
        if User._company_id_ == company_id:
            return
        
        if User._Mapper_ is not None:
            User._Mapper_.dispose()
                    
        metaData = MetaData()
        tableDefine = Table('user', metaData,
                          Column('id', Integer, primary_key = True),
                          Column('name', String),
                          Column('realname', String),
                          Column('pwd', String),
                          Column('shop_id', Integer),
                          Column('phone', String),
                          Column('email', String),
                          Column('right_level', Integer),                          
                           schema = "company_%s" % company_id)
        
        User._Mapper_  = mapper(User, tableDefine)
        
